package com.ajzhang.facade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ajzhang.bean.UserInfo;

public class UserInfoFacade extends AbstractFacade {

	@Transactional(readOnly = true)
	public List<UserInfo> findPageByCriteria(int pagerOffset, int pageSizeKey, DetachedCriteria detachedCriteria) throws Exception {
		List list = this.baseDao.findPageByCriteriaOrderBy(detachedCriteria, pagerOffset, pageSizeKey);
		return list;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void save(UserInfo userInfo) {
		this.baseDao.insert(userInfo);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void update(UserInfo userInfo) {
		this.baseDao.update(userInfo);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void delete(UserInfo userInfo) {
		this.baseDao.delete(userInfo);
	}

	@Transactional(readOnly = true)
	public UserInfo findUserInfoById(Integer id) throws Exception {
		List<UserInfo> list = new ArrayList<UserInfo>();
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserInfo.class);
		detachedCriteria.add(Expression.eq("userId", id));
		list = this.baseDao.findByCriteria(detachedCriteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	@Transactional(readOnly = true)
	public UserInfo findUserInfoByuserName(String userName) throws Exception {
		List<UserInfo> list = new ArrayList<UserInfo>();
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserInfo.class);
		detachedCriteria.add(Expression.eq("userName", userName));
		list = this.baseDao.findByCriteria(detachedCriteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
}
